package com.xywy.me;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.app.MainApplication;
import com.xywy.common.greendao.BaseDao;
import com.xywy.common.util.ble.util.BLEBloodPresureProvider;
import com.xywy.common.util.ble.util.BLEWeightScaleProvider;
import com.xywy.common.widget.TitleBar2;
import com.xywy.me.bean.User;
import com.xywy.me.bean.UserKeeper;
import com.xywy.me.widget.ExitPopupWindow;
import com.xywy.start.MainActivity;
import com.xywy.start.SexActivity;
import com.xywy.wjk.dao.BloodPressureDataDao;
import com.xywy.wjk.dao.WeightDataDao;
import com.xywy.wjk.util.DeviceState;
import com.xywy.wjk.widget.TipDialog;
import com.xywy.wjk.widget.TipDialog.CancleListener;
import com.xywy.wjk.widget.TipDialog.ConfirmListener;

/**
 * 我 - 设置 - 账户[手机号码、邮箱地址、密码、退出登录]
 * 
 * @author xiaogd
 * 
 */
public class SetupAccountActivity extends BaseActivity {
	private static final String TAG = "SetupAccountActivity";
	private Context mContext;
	private String mobile = "";
	private String userid = "";
	private TextView tvMobile;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置为全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.me_setup_account);

		this.preInit();
		this.initView();
		this.loadData();
		// this.setListener();
	}

	public void startOnClick(View view) {
		switch (view.getId()) {
		case R.id.bt_height_next:
			next();
			break;
		}
		;
	}

	private void next() {
		Intent intent = new Intent(this, SexActivity.class);
		startActivity(intent);
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	@Override
	public void preInit() {
		mContext = this.context;

		// 取出本地mobile
		SharedPreferences sp = context.getSharedPreferences("config",
				Context.MODE_PRIVATE);
		mobile = sp.getString("mobile", "");
		userid = UserKeeper.getCurrentUser(this).getUserid();
	}

	@Override
	public void loadData() {

	}

	@Override
	public void initView() {
		// title
		TitleBar2 titleBar = (TitleBar2) findViewById(R.id.me_setup_ac_title_bar);
		titleBar.onClickBack(this);
		TextView tvTitle = titleBar.getTitle();
		TextView tvBack = titleBar.getTvBack();
		titleBar.getGo().setVisibility(View.INVISIBLE);
		tvTitle.setText(getString(R.string.me_setup_account));
		tvBack.setText(getString(R.string.me_set));
		tvMobile = (TextView) findViewById(R.id.tv_me_setup_account_mobile);
		tvMobile.setText(mobile);

	}

	public void onClcikSetUpAccount(View view) {
		switch (view.getId()) {
		case R.id.rl_me_setup_account_mobile:
			intentForword(SetupAcMobileActivity.class);
			break;
		case R.id.rl_me_setup_account_email:
			intentForword(SetupAcEmailActivity.class);
			break;
		case R.id.rl_me_setup_account_pwd:
			intentForword(SetupVerificationPasswordActivity.class);
			break;
		case R.id.rl_me_setup_accout_exit:
			// TODO:执行退出方法后，再执行断开蓝牙连接
			exit();
			break;
		}
	}

	/*
	 * 断开ble连接
	 */
	private void disConnectBLE() {
		MainApplication app = MainApplication.getInstance();
		BluetoothAdapter mBluetoothAdapter = app.getWeightBleAdapter();
		BLEWeightScaleProvider WCutils = app.getWeightBleProvider();
		BLEBloodPresureProvider BPutils = app.getBloodPBleProvider();
		if (WCutils != null) { // 当前设备正在连接中
			WCutils.disconnect(); // 先断开设备连接
		}
		if (BPutils != null) {
			BPutils.disconnect(); // 先断开设备连接
		}

	}

	WeightDataDao weightDataDao;
	BloodPressureDataDao bloodPressureDataDao;

	/**
	 * 只要点击退出，清理本地数据；
	 */
	private void clearTable() {
		User user = UserKeeper.getCurrentUser(this);
		UserKeeper.logOut(user);
//		BaseDao baseDao = BaseDao.getInstance(SetupAccountActivity.this);
//		weightDataDao = baseDao.getWeightDataDao();
//		bloodPressureDataDao = baseDao.getBloodPressureDataDao();
//		weightDataDao.deleteAll();
//		bloodPressureDataDao.deleteAll();
	}

	private void intentForword(Class cl) {
		Intent intent = new Intent(mContext, cl);
		intent.putExtra("mobile", mobile);
		intent.putExtra("userid", userid);
		startActivity(intent);
	}

	/**
	 * 退出事件，弹出popupWindow
	 */
	private ExitPopupWindow window;

	private void exit() {
		// // 实例化SelectPicPopupWindow
		// window = new ExitPopupWindow(this, itemsOnClick);
		// // 显示窗口
		// window.showAtLocation(this.findViewById(R.id.ll_me_acount),
		// Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //
		// 设置layout在PopupWindow中显示的位置

		final TipDialog dialog = new TipDialog(this, TipDialog.VIEW_DIALOG);
		dialog.setTitleAndContent("退出", "退出当前账号");
		dialog.setConfirmCallback(new ConfirmListener() {

			@Override
			public void confirm() {
				Intent intent = new Intent(mContext, MainActivity.class);
				disConnectBLE();
				clearTable();
				deleteSp();
				SharedPreferences sp = getSharedPreferences("config",
						Context.MODE_PRIVATE);
				Editor editor = sp.edit();
				editor.putString("password", "");
				editor.putString("userid", "");
				editor.commit();

				finish();

				MainApplication amp = (MainApplication) context
						.getApplicationContext();
				amp.exit();
				dialog.dismiss();
				mContext.startActivity(intent);
			}

		});
		dialog.setCancleCallback(new CancleListener() {

			@Override
			public void cancle() {
				dialog.dismiss();
			}
		});
		dialog.show();
	}

	// 为弹出窗口实现监听类
	private View.OnClickListener itemsOnClick = new View.OnClickListener() {

		public void onClick(View v) {
			window.dismiss();
			switch (v.getId()) {
			case R.id.btn_me_account_exit: // 确认退出
				// Intent intent = new Intent(mContext, MainActivity.class);
				// SharedPreferences sp = getSharedPreferences("config",
				// Context.MODE_PRIVATE);
				// Editor editor = sp.edit();
				// editor.putString("password", "");
				// editor.putString("userid", "");
				// editor.commit();
				// mContext.startActivity(intent);
				// finish();
				// MainApplication amp = (MainApplication) context
				// .getApplicationContext();
				// amp.exit();
				break;
			default:
				break;
			}
		}

	};

	private void deleteSp() {
		SharedPreferences weightSp = getSharedPreferences(DeviceState.DEVICE_WEIGHT_SCALE, MODE_PRIVATE);
		weightSp.edit().clear().commit();
		
		SharedPreferences bloodSp = getSharedPreferences(DeviceState.DEVICE_BLOOD_PAUSERE, MODE_PRIVATE);
		bloodSp.edit().clear().commit();
		
		
		SharedPreferences sp = getSharedPreferences("config",
				Context.MODE_PRIVATE);
		String mobile = sp.getString("mobile", "");
		Editor editor = sp.edit();
		editor.clear();
		editor.commit();
		editor = sp.edit();
		editor.putString("mobile", mobile);
		editor.commit();
		
	}
}
